package com.zzyl.nursing.controller.member;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.DeviceDataDto;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.dto.FamilyMemberDto;
import com.zzyl.nursing.dto.UserLoginDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.vo.DeviceDataPointVo;
import com.zzyl.nursing.vo.FamilyMemberElderVo;
import com.zzyl.nursing.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 老人家属Controller
 * 
 * @author ruoyi
 * @date 2025-10-22
 */
@RestController
@RequestMapping("/member/user")
@Api(tags = "老人家属相关接口")
public class FamilyMemberController extends BaseController
{
    @Autowired
    private IFamilyMemberService familyMemberService;
    @Autowired
    private IFamilyMemberElderService familyMemberElderService;
    @Autowired
    private IDeviceService deviceService;

    /**
     * 用户端-微信小程序登录接口
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody UserLoginDto dto) {

        LoginVo loginVo = familyMemberService.login(dto);
        return success(loginVo);
    }

    /**
     * 查询老人家属列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:FamilyMember:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取老人家属列表")
    public TableDataInfo<List<FamilyMember>> list(FamilyMember familyMember)
    {
        startPage();
        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
        return getDataTable(list);
    }

    /**
     * 导出老人家属列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:FamilyMember:export')")
    @Log(title = "老人家属", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出老人家属列表")
    public void export(HttpServletResponse response, FamilyMember familyMember)
    {
        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
        ExcelUtil<FamilyMember> util = new ExcelUtil<FamilyMember>(FamilyMember.class);
        util.exportExcel(response, list, "老人家属数据");
    }

    /**
     * 获取老人家属详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:FamilyMember:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取老人家属详细信息")
    public R<FamilyMember> getInfo(@ApiParam(value = "老人家属ID", required = true)
            @PathVariable("id") Long id)
    {
        return R.ok(familyMemberService.selectFamilyMemberById(id));
    }

    /**
     * 新增老人家属
     */
    @PreAuthorize("@ss.hasPermi('nursing:FamilyMember:add')")
    @Log(title = "老人家属", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增老人家属")
    public AjaxResult add(@ApiParam(value = "老人家属实体", required = true) @RequestBody FamilyMember familyMember)
    {
        return toAjax(familyMemberService.insertFamilyMember(familyMember));
    }

    /**
     * 修改老人家属
     */
    @PreAuthorize("@ss.hasPermi('nursing:FamilyMember:edit')")
    @Log(title = "老人家属", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改老人家属")
    public AjaxResult edit(@ApiParam(value = "老人家属实体", required = true) @RequestBody FamilyMember familyMember)
    {
        return toAjax(familyMemberService.updateFamilyMember(familyMember));
    }

    /**
     * 删除老人家属
     */
    @PreAuthorize("@ss.hasPermi('nursing:FamilyMember:remove')")
    @Log(title = "老人家属", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除老人家属")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids)
    {
        return toAjax(familyMemberService.deleteFamilyMemberByIds(ids));
    }

    /**
     * 小程序绑定家人
     *
     * @param
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("绑定家人")
    public AjaxResult add(@RequestBody FamilyMemberDto dto) {
        familyMemberService.add(dto);
        return success();
    }

    @DeleteMapping("/deleteById")
    @ApiOperation("解绑")
    public AjaxResult deleteById(Long id) {
        familyMemberElderService.deleteById(id);
        return success();
    }
    /**
     * 查询家人列表
     */
    @GetMapping("/my")
    @ApiOperation("查询家人列表")
    public AjaxResult myFamilyMember() {
        List<FamilyMemberElder> mylist = familyMemberElderService.mylist();
        return AjaxResult.success(mylist);
    }
    /**
     * 1.3 查看家人列表（分页查询）
     */
    @GetMapping("/list-by-page")
    @ApiOperation("查询家人信息")
    public AjaxResult listByPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        startPage();
        List<FamilyMemberElderVo> familyMemberElderListVo = familyMemberElderService.listByPage();
        return success(familyMemberElderListVo);
    }
    /**
     * 查询健康数据
     */
    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation("查询健康数据")
    public AjaxResult queryDevicePropertyStatus(@RequestBody DeviceDto dto) {
        Map<String, Object> resultMap = deviceService.QueryDevicePropertyStatus(dto);
        return AjaxResult.success(resultMap);
    }

    /**
     * 按天统计查询指标数据
     * 统计某个设备下，某个物模型id，在一天内每小时对应的平均值
     * 找不到小程序按键在哪，测试类测试通过
     * @return
     */
    @GetMapping("/queryDeviceDataListByDay")
    @ApiOperation("按天统计查询指标数据")
    public AjaxResult queryDeviceDataListByDay(DeviceDataDto dto){
        List<DeviceDataPointVo> list = familyMemberElderService.queryDeviceDataListByDay(dto);
        return AjaxResult.success(list);
    }

    /**
     * 1.6 按周统计查询指标数据
     * 统计某个设备下，某个物模型id，在几天内每天对应的平均值
     * @return
     */
    @GetMapping("/queryDeviceDataListByWeek")
    @ApiOperation("按天统计查询指标数据")
    public AjaxResult queryDeviceDataListByWeek(DeviceDataDto dto){
        List<DeviceDataPointVo> list = familyMemberElderService.queryDeviceDataListByWeek(dto);
        return AjaxResult.success(list);
    }

    @GetMapping("/pageQueryAlertData")
    public AjaxResult pageQueryAlertData(DeviceDataDto dto){
        startPage();
        List<DeviceData> list = familyMemberElderService.pageQueryAlertData(dto);
        return AjaxResult.success(list);
    }

}
